package wyp.library.tools;

import wyp.library.ui.activity.WypActivity;
import android.content.Context;
import android.graphics.Point;
import android.widget.LinearLayout.LayoutParams;

public class WypSizeTool {

	public static final String TAG = WypSizeTool.class.getSimpleName();

	public static final int L_DPI = 120;
	public static final int M_DPI = 160;
	public static final int H_DPI = 240;
	public static final int XH_DPI = 320;
	public static final int XXH_DPI = 480;
	public static final int XXXH_DPI = 640;
	public static final int MATCH_PARENT;
	public static final int WRAP_CONTENT;

	static {
		if (WypSystemTool.getSdkVersion() < 3) {
			MATCH_PARENT = LayoutParams.FILL_PARENT;
		} else {
			MATCH_PARENT = LayoutParams.MATCH_PARENT;
		}
		WRAP_CONTENT = LayoutParams.WRAP_CONTENT;
	}

	public static void main(String[] args) {
		float _density = 2;// getDensity(4, 1280, 720);
		getDp(_density, 98, 0);
	}

	public static void getDp(float _density, int _xPx, int _yPx) {
		float _dpX = _xPx / _density;
		float _dpY = _yPx / _density;
		_dpX = (float) (Math.round(_dpX * 10) / 10.0);
		_dpY = (float) (Math.round(_dpY * 10) / 10.0);
		System.out.println("DP：" + Math.round(_dpX) + " x " + Math.round(_dpY));
	}

	/**
	 * ldpi = 0.00625 * 120
	 * 
	 * 0.75 mdpi = 0.00625 * 160
	 * 
	 * 1.0 hdpi = 0.00625 * 240
	 * 
	 * 1.5 xhdpi = 0.00625 * 320
	 * 
	 * 2.0 xxhdpi = 0.00625 * 480
	 * 
	 * 3.0 xxxhdpi = 0.00625 * 640 -> 4.0
	 * 
	 * @param _inch
	 * @param _screenPx_x
	 * @param _screenPx_y
	 * @return
	 */
	public static float getDensity(double _inch, int _screenPx_x, int _screenPx_y) {
		float _dpi = (float) (Math.sqrt(_screenPx_x * _screenPx_x + _screenPx_y * _screenPx_y) / _inch);
		System.out.print("实际dpi=" + _dpi);
		if (_dpi >= XXXH_DPI) {
			_dpi = XXXH_DPI;
		} else if (_dpi >= XXH_DPI) {
			_dpi = XXH_DPI;
		} else if (_dpi >= XH_DPI) {
			_dpi = XH_DPI;
		} else if (_dpi >= H_DPI) {
			_dpi = H_DPI;
		} else if (_dpi >= M_DPI) {
			_dpi = M_DPI;
		} else if (_dpi >= L_DPI) {
			_dpi = L_DPI;
		}

		float _density = _dpi / 160;
		System.out.println(", 标准dpi=" + _dpi + ", density=" + _density);
		return _density;
	}

	public static int dp2px(Context context, float _dpValue) {
		float _density = context.getResources().getDisplayMetrics().density;
		int _pxValue = (int) (_dpValue * _density + 0.5f);
		return _pxValue;
	}

	public static int px2dp(Context context, float _pxValue) {
		float _density = context.getResources().getDisplayMetrics().density;
		int _dpValue = (int) (_pxValue / _density + 0.5);
		return _dpValue;
	}

	public static int sp2px(Context __context, float __spValue) {
		float __fontScale = __context.getResources().getDisplayMetrics().scaledDensity;
		return (int) (__spValue * __fontScale + 0.5f);
	}


	public static int getScreenWidth(WypActivity _activity) {
		int _width = 0;
		if (WypSystemTool.getSdkVersion() < 13) {
			_width = _activity.getWindowManager().getDefaultDisplay().getWidth();
		} else {
			Point _size = new Point();
			_activity.getWindowManager().getDefaultDisplay().getSize(_size);
			_width = _size.x;
		}
		return _width;
	}
	public static int getScreenHeight(WypActivity _activity) {
		int _height = 0;
		if (WypSystemTool.getSdkVersion() < 13) {
			_height = _activity.getWindowManager().getDefaultDisplay().getHeight();
		} else {
			Point _size = new Point();
			_activity.getWindowManager().getDefaultDisplay().getSize(_size);
			_height = _size.y;
		}
		return _height;
	}

}
